#include "playscene.h"
#include <QDebug>
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <QSound>
PlayScene::PlayScene(int levelNum)
{
    QString str =QString("第 %1 关").arg(levelNum);
    //qDebug()<<str;
    this->levelIndex = levelNum;

    //初始化游戏场景
    //设置固定大小
    this->setFixedSize(600,888);

    //设置图标
    this->setWindowIcon(QPixmap(":/new/prefix1/res/Coin0001.png"));
    this->setWindowTitle(QString(str));

    //创建菜单栏
    QMenuBar *bar= menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu *startMenu =bar->addMenu("开始");

    //创建退出的菜单项
    QAction *quitAction=startMenu->addAction("退出");

    //点击退出
    connect(quitAction,&QAction::triggered,[=]{
        this->close();
    });

    QSound *backSound = new QSound(":/new/prefix1/res/BackButtonSound.wav");
    //返回按钮
    MyPushButton *backBtn = new MyPushButton(":/new/prefix1/res/BackButton.png",":/new/prefix1/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    //点击返回
    connect(backBtn,&MyPushButton::clicked,[=]{
        //qDebug()<<"点了返回按钮";
        //告诉主场景，我返回了，主场景监听ChooseLevelScene的返回按钮
        backSound->play();
        QTimer::singleShot(250,this,[=](){
            emit this->ChooseSceneBack();
        });
    });

    //显示当前关卡数
    QLabel *label =new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString str1 =QString ("Level: %1").arg(this->levelIndex);
    //将字体设置在标签控件中
    label->setFont(font);
    label->setText(str1);
    label->setGeometry(30,this->height()-50,240,50);

    dataConfig config;
    //初始化每个关卡的二维数组
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
        }
    }

    //胜利图片显示
    QLabel *winLabel = new QLabel;
    QPixmap tempPix;
    tempPix.load(":/new/prefix1/res/LevelCompletedDialogBg.png");
    tempPix=tempPix.scaled(tempPix.width()*2,tempPix.height()*2);
    winLabel->setGeometry(0,0,tempPix.width(),tempPix.height());
    winLabel->setPixmap(tempPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tempPix.width())*0.5,-45-tempPix.height());

    QSound *filpSound =new QSound(":/new/prefix1/res/ConFlipSound.wav");
    QSound *winSound = new QSound(":/new/prefix1/res/LevelWinSound.wav");

    //显示金币背景图案
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            //绘制背景图片
            QPixmap pix = QPixmap(":/new/prefix1/res/BoardNode(1).png");
            pix=pix.scaled(pix.width()*2,pix.height()*2);
            QLabel * label = new QLabel;
            label ->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move((this->width()-4*110)*0.5+i*110,j*110+(this->height()-4*110)*0.5);

            //创建金币
            if(this->gameArray[i][j]==1)
            {
                //显示金币
                str=":/new/prefix1/res/Coin0001.png";
            }
            else
            {
                str=":/new/prefix1/res/Coin0008.png";
            }
            MyCoin * coin =new MyCoin(str);
            coin->setParent(this);
            coin->move((this->width()-4*110)*0.5+i*110+(110-coin->width())*0.25,j*110+(this->height()-4*110)*0.5+(110-coin->height())*0.25);

            //给金币属性赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->gameArray[i][j];//1正面 0反面

            //维护金币二维数组
            coinBtn[i][j]=coin;

            //点击金币，进行翻转
            connect(coin,&MyCoin::clicked,[=](){
                filpSound->play();
                coin->changeFlag();
                this->gameArray[i][j] = this ->gameArray[i][j]==0?1:0;

                //翻转周围的金币
                QTimer::singleShot(300,this,[=](){
                    if(coin->posX+1<4)
                    {
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY] = this ->gameArray[coin->posX+1][coin->posY]==0?1:0;
                    }
                    if(coin->posX-1>=0)
                    {
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY] = this ->gameArray[coin->posX-1][coin->posY]==0?1:0;
                    }
                    if(coin->posY+1<4)
                    {
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1] = this ->gameArray[coin->posX][coin->posY+1]==0?1:0;
                    }
                    if(coin->posY-1>=0)
                    {
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1] = this ->gameArray[coin->posX][coin->posY-1]==0?1:0;
                    }
                    //延时器是异步的，后面的代码先执行，再回来执行延时器
                    this->isWin = true;
                    for(int i=0;i<4;i++)
                    {
                        for(int j=0;j<4;j++)
                        {
                            if(coinBtn[i][j]->flag==false)
                            {
                                this->isWin=false;
                                break;
                            }
                        }
                    }
                    if(this->isWin==true)
                    {
                        QTimer::singleShot(300,this,[=](){

                        });
                        winSound->play();
//                        qDebug()<<"游戏胜利";
                        //将所有的按钮的胜利标志改为胜利
                        for(int i=0;i<4;i++)
                        {
                            for(int j=0;j<4;j++)
                            {
                                coinBtn[i][j]->isWin = true;
                            }
                        }

                        //将胜利的图片砸下来
                        QPropertyAnimation * animation =new QPropertyAnimation(winLabel,"geometry");
                        //设置时间间隔
                        animation->setDuration(1000);
                        //设置开始位置
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        //设置结束位置
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+100+tempPix.height(),winLabel->width(),winLabel->height()));
                        //设置缓和曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        animation->start();
                    }
                });

            });

        }
    }

}
void PlayScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    QPixmap pix;
    pix.load(":/new/prefix1/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //画背景上的图标
    pix.load(":/new/prefix1/res/Title.png");

    //缩放图标
    //pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);

    painter.drawPixmap(10,30,pix);
}
